﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Core.DatabaseAccess;

namespace PuroGame.Repositories.Repository
{
    public class JogosRepository : Interfaces.IJogosRepository
    {
        public List<Business.Jogos> List(int top)
        {
            throw new NotImplementedException();
        }

        public List<Business.Jogos> Paginate(int perpage, int page, string orderby)
        {
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("@Top", perpage);
            param.Add("@Page", page);
            param.Add("@OrderBy", orderby);

            string sQuery =
            @"
                ;WITH Jogos_CTE AS (SELECT TOP ((@Top * @Page) + @Top) ROW_NUMBER() OVER (ORDER BY codigo) as Row, * 
                FROM jogos WHERE codigo_sistema = 1)

                SELECT * FROM Jogos_CTE WHERE [Row] BETWEEN (((@Top*@Page)-@Top)+1) AND (@Top * @Page)
            ";

            return Base.Query<Business.Jogos>(sQuery, param).ToList();
        }

        public List<Business.Jogos> AutoComplete(string texto, int top)
        {
            throw new NotImplementedException();
        }

        public Business.Jogos GetById(int id)
        {
            Dictionary<string, object> param = new Dictionary<string, object>();
            param.Add("codigo", id);
            return Base.Query<Business.Jogos>("SELECT * FROM jogos WHERE codigo = @codigo", param).FirstOrDefault();
        }
    }
}
